package com.tshopping.service.impl;

import com.tshopping.bean.Page;
import com.tshopping.bean.Store;
import com.tshopping.bean.StoreCollection;
import com.tshopping.dao.StoreCollectionDao;
import com.tshopping.dao.StoreDao;
import com.tshopping.service.StoreCollectionService;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * @name: StoreCollectionServiceImpl
 * @description: 收藏店铺业务层类
 * @author: Jackeys
 * @date: 2021-01-29 11:41
 */
@Service(value = "storeCollectionServiceImpl")
@Transactional
public class StoreCollectionServiceImpl implements StoreCollectionService {

    @Autowired
    private StoreCollectionDao storeCollectionDao;

    @Autowired
    private StoreDao storeDao;

    @Override
    /**
     * @description 处理用户收藏店铺
     * @Param: user
     * @Param: store
     * @Param: time
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 11:44
     */
    public ResponseData addStoreCollection(int user, int store, String time) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", storeCollectionDao.insertStoreCollection(user, store, time));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户取消店铺收藏
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 11:45
     */
    public ResponseData deleteStoreCollectionById(int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", storeCollectionDao.deleteStoreCollectionById(id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户返回店铺收藏
     * @Param: user
     * @Param: start
     * @Param: size
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 11:46
     */
    public ResponseData selectStoreCollectionList(int user, int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 10;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = storeCollectionDao.selectCountByUserId(user);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        ArrayList<StoreCollection> storeCollections=storeCollectionDao.selectStoreCollectionsByUserId(user, start, size);
        ArrayList<Store> storeList=new ArrayList<>();
        for(StoreCollection storeCollection:storeCollections){
            storeList.add(storeDao.selectStoreByStoreId(storeCollection.getStore()));
        }
        responseData.putDataValue("storeList", storeList);
        responseData.putDataValue("page", page);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户判断收藏
     * @Param: goods
     * @Param: user
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 11:56
     */
    public ResponseData selectCount(int store, int user) {
        ResponseData responseData=ResponseData.ok();
        boolean flag;
        if(storeCollectionDao.selectCountByStoreAndUser(store,user)==1){
            flag=true;
        }else{
            flag=false;
        }
        responseData.putDataValue("flag",flag);
        return responseData;
    }

    @Override
    /**
     * @description 用户删除店铺收藏
     * @Param: user
     * @Param: store
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 16:35
     */
    public ResponseData deleteStoreCollectionByUserAndStore(int user, int store) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",storeCollectionDao.deleteStoreCollectionByUserAndStore(user,store));
        return responseData;
    }
}
